#!/usr/bin/env python
# -*- encoding: utf-8 -*-
'''
@File    :   App_ClearDiagInfo_0x14_test.py
@Time    :   2022/08/29 13:31:28
@Author  :   YangLiang 
@Version :   1.0
@Contact :   yangliang@chengtech.com
@License :   (C)Copyright 2022-2025
@Desc    :   None
'''

# here put the import lib
from pytest_assume.plugin import assume
import Set_up 
import pytest
import time
import requests
from sympy import I
# from sympy import assuming
from commDriver.TS_CAN import *
import allure
import sys
from collections.abc import Iterable

from sqlalchemy import false
sys.dont_write_bytecode = True

ClearDtcInfo_NRC_Phy = [([0x14],[0x7F,0x14,0x13]),                  
                        ([0x14,0xFF],[0x7F,0x14,0x13]),
                        ([0x14,0xFF,0xFF],[0x7F,0x14,0x13]),
                        ([0x14,0xFF,0xFF,0xFF,0xFF],[0x7F,0x14,0x13]),
                        ([0x14,0x11,0x22,0xFF,0xFF],[0x7F,0x14,0x13]),
                        ([0x14,0xFF,0xFF,0xFE],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x29,0x87],[0x7F,0x14,0x31]),  # According to project DTC list def: This line goes down
                        ([0x14,0x5C,0x11,0x83],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x12,0x83],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x13,0x82],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x14,0x82],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x15,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x16,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x01,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x17,0x83],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x18,0x82],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x19,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x84,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0xC1,0x40,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x1A,0x81],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x00,0x88],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x01,0x88],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x02,0x87],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x03,0x17],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x04,0x16],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x05,0x17],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x06,0x16],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x07,0x54],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x08,0x54],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x09,0x4B],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0A,0x4B],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0B,0x02],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0C,0x02],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0D,0x44],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0E,0x44],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x0F,0x4A],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x10,0x4A],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x1B,0x4A],[0x7F,0x14,0x31]),
                        ([0x14,0x5C,0x1C,0x4A],[0x7F,0x14,0x31])]

ClearDtcInfo_No_Resp_Fun = [([0x14,0xFF,0xFF,0xFE],[]),
                             ([0x14,0xC1,0x29,0x87],[]),  # According to project DTC list def: This line goes down
                             ([0x14,0x5C,0x11,0x83],[]),
                             ([0x14,0x5C,0x12,0x83],[]),
                             ([0x14,0x5C,0x13,0x82],[]),
                             ([0x14,0x5C,0x14,0x82],[]),
                             ([0x14,0x5C,0x15,0x81],[]),
                             ([0x14,0x5C,0x16,0x81],[]),
                             ([0x14,0xC1,0x01,0x87],[]),
                             ([0x14,0x5C,0x17,0x83],[]),
                             ([0x14,0x5C,0x18,0x82],[]),
                             ([0x14,0x5C,0x19,0x81],[]),
                             ([0x14,0xC1,0x84,0x87],[]),
                             ([0x14,0xC1,0x40,0x87],[]),
                             ([0x14,0x5C,0x1A,0x81],[]),
                             ([0x14,0x5C,0x00,0x88],[]),
                             ([0x14,0x5C,0x01,0x88],[]),
                             ([0x14,0x5C,0x02,0x87],[]),
                             ([0x14,0x5C,0x03,0x17],[]),
                             ([0x14,0x5C,0x04,0x16],[]),
                             ([0x14,0x5C,0x05,0x17],[]),
                             ([0x14,0x5C,0x06,0x16],[]),
                             ([0x14,0x5C,0x07,0x54],[]),
                             ([0x14,0x5C,0x08,0x54],[]),
                             ([0x14,0x5C,0x09,0x4B],[]),
                             ([0x14,0x5C,0x0A,0x4B],[]),
                             ([0x14,0x5C,0x0B,0x02],[]),
                             ([0x14,0x5C,0x0C,0x02],[]),
                             ([0x14,0x5C,0x0D,0x44],[]),
                             ([0x14,0x5C,0x0E,0x44],[]),
                             ([0x14,0x5C,0x0F,0x4A],[]),
                             ([0x14,0x5C,0x10,0x4A],[]),
                             ([0x14,0x5C,0x1B,0x4A],[]),
                             ([0x14,0x5C,0x1C,0x4A],[])]

# 20220913 Revise:BYD 企标规定 功能寻址 NRC=11 12 13 7E 7F 31都不响应
ClearDtcInfo_NRC13_Fun = [([0x14],[]),                           #[0x7F,0x14,0x13]),                  
                           ([0x14,0xFF],[]),                     #[0x7F,0x14,0x13]),
                           ([0x14,0xFF,0xFF],[]),                #[0x7F,0x14,0x13]),
                           ([0x14,0xFF,0xFF,0xFF,0xFF],[]),      #[0x7F,0x14,0x13]),
                           ([0x14,0x11,0x22,0xFF,0xFF],[])]      #[0x7F,0x14,0x13])]

class Test_ClearDiagInfo_Phy(object):
      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test with physical addressing')
      @allure.description('Test positive response of 0x14 in default session with physical addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_ClearDiagInfo_Default_Phy(self,action,Driver):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 1, iStep, False)

      @allure.severity('normal')
      @allure.description('Test positive response of 0x14 in extended session with physical addressing')
      def test_ClearDiagInfo_Extended_Phy(self,action,Driver,extend_session):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False)

     
      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC_Phy]
      @allure.severity('minor')
      @allure.description('Test NRC of 0x14 in default session with physical addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC_Phy,ids = ids)
      def test_ClearDiagInfo_NRC_Default_Phy(self,action,Driver,req,resp):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 1, iStep, False)


      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC_Phy]
      @allure.severity('minor')
      @allure.description('Test NRC of 0x14 in extended session with physical addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC_Phy,ids = ids)
      def test_ClearDiagInfo_NRC_Extended_Phy(self,action,Driver,req,resp):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, resp, 3, iStep, False)

      @allure.severity('minor')
      @allure.description('Test NRC7F of 0x14 in programming session with physical addressing')
      def test_ClearDiagInfo_NRC7F_Programming_Phy(self,action,Driver):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x7F,0x14,0x7F], 2, iStep, False)


class Test_ClearDiagInfo_Fun(object):
      @allure.severity('normal')
      @allure.story('Clear Diagnostic information Test with function addressing')
      @allure.description('Test positive response of 0x14 in default session with function addressing')
      @allure.testcase('https://chengtech.pingcode.com/testhub/libraries/630357052177ea2394680877/cases')
      def test_ClearDiagInfo_Default_Fun(self,action,Driver):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 1, iStep, False, 'UDSFun', 'noClearFault')

      @allure.severity('normal')
      @allure.description('Test positive response of 0x14 in extended session with function addressing')
      def test_ClearDiagInfo_Extended_Fun(self,action,Driver):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [0x54], 3, iStep, False, 'UDSFun', 'noClearFault')

      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC13_Fun]
      @allure.severity('minor')
      @allure.description('Test NRC13 of 0x14 in default session with function addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13_Fun,ids = ids)
      def test_ClearDiagInfo_NRC13_Default_Fun(self,action,Driver,req,resp):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 1, iStep, False, 'UDSFun', 'noClearFault')



      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_No_Resp_Fun]
      @allure.severity('minor')
      @allure.description('Test no response of 0x14 in default session with function addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_No_Resp_Fun,ids = ids)
      def test_ClearDiagInfo_No_Resp_Default_Fun(self,action,Driver,req,resp):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 1, iStep, False, 'UDSFun', 'noClearFault')

      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_NRC13_Fun]
      @allure.severity('minor')
      @allure.description('Test NRC13 of 0x14 in extended session with function addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_NRC13_Fun,ids = ids)
      def test_ClearDiagInfo_NRC13_Extended_Fun(self,action,Driver,req,resp):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 3, iStep, False, 'UDSFun', 'noClearFault')


      ids = ['Request {0}'.format(binascii.b2a_hex(bytearray(req),' ')) for req,resp in ClearDtcInfo_No_Resp_Fun]
      @allure.severity('minor')
      @allure.description('Test no response of 0x14 in extended session with function addressing')
      @pytest.mark.parametrize('req,resp', ClearDtcInfo_No_Resp_Fun,ids = ids)
      def test_ClearDiagInfo_No_Resp_Extended_Fun(self,action,Driver,req,resp):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, req, [], 3, iStep, False, 'UDSFun', 'noClearFault')

      @allure.severity('minor')
      @allure.description('Test NRC7F of 0x14 in programming session with function addressing')
      def test_ClearDiagInfo_NRC7F_Programming_Fun(self,action,Driver):
         time.sleep(0.5)
         iStep = 1
         iStep,uds_resp, size = Driver['CAN'].checkReqResp(Driver, [0x14,0xFF,0xFF,0xFF], [], 2, iStep, False, 'UDSFun', 'noClearFault')
     